let DBConfig = require("../config/DBConfig_hxm");

//-------------------HXM接口---体检报告信息-----------------------体检报告信息---------------------------------
module.exports = {   //获取我的体检列表
  async selectUserInfo(req, resp) {
    let { userName } = req.body;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "select * from ordertable where user_tel=?",
        [req.query.user_tel],
        (err, data) => {
          console.log('列表',data);
          if (!err) {
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    }).catch(() => {});
    resp.send(userInfo);
  },

  async getaccomplishphysical(req, resp) {
    //体检详情
    let { userName } = req.body;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "select * from ordertable join physical on ordertable.phy_id=physical.phy_id  join set_meal on ordertable.productId=set_meal.productId where order_orderId=?",
        [req.query.order_orderId],
        (err, data) => {
          console.log('体检机构', data);
          if (!err) {
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    }).catch(() => {});
    resp.send(userInfo);
  },

  async getlookreport(req, resp) {
    //体检详情
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db("SELECT * FROM report JOIN register ON report.user_id=register.user_id WHERE order_orderId=?", [req.query.order_orderId], (err, data) => {
                console.log('体检详情',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getlookreportpc(req, resp) {  //体检详情pc
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select * from report', [], (err, data) => {
                console.log('体检详情pc',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getlookreportpcuser(req, resp) {  //体检详情pc段对应user
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select * from report where user_id=?', [req.query.user_id], (err, data) => {
                console.log('体检详情pc-user',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async addlookreportpcuser(req, resp) {  //体检详情pc段对应user添加
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('INSERT INTO report VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [req.query.user_name,req.query.DBP,req.query.SBP,req.query.BP,req.query.health,req.query.hemameba,req.query.red_blood,req.query.hemoglobin,req.query.HCT,req.query.opinionFirst,req.query.ALT,req.query.AST,req.query.GGT,req.query.opinionSecond,req.query.pulse,req.query.indexa,req.query.conclusion,req.query.order_orderId,req.query.org_id], (err, data) => {
                console.log('添加',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async editlookreportpcuser(req, resp) {  //体检详情pc段对应user修改
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('UPDATE  report set user_name=?,DBP=?,SBP=?,BP=?,health=?,hemameba=?,red_blood=?,hemoglobin=?,HCT=?,opinionFirst=?,ALT=?,AST=?,GGT=?,opinionSecond=?,pulse=?,indexa=?,conclusion=?,order_orderId=? where user_id=?', [req.query.user_name,req.query.DBP,req.query.SBP,req.query.BP,req.query.health,req.query.hemameba,req.query.red_blood,req.query.hemoglobin,req.query.HCT,req.query.opinionFirst,req.query.ALT,req.query.AST,req.query.GGT,req.query.opinionSecond,req.query.pulse,req.query.indexa,req.query.conclusion,req.query.order_orderId,req.query.user_id], (err, data) => {
                console.log('修改',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getreport(req, resp) {  //体检报告
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select * from ordertable where user_tel=? and order_status=?', [req.query.user_tel,req.query.order_status], (err, data) => {
                console.log('查询',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getdim(req, resp) {  //模糊查询
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db(
                'select * from report where user_name=?', [req.query.user_name], (err, data) => {
                console.log('查询',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getadress(req, resp) {  //机构地址
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select * from organization', [], (err, data) => {
                console.log('机构查询',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async delreportpc(req, resp) {  //单项删除
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('delete from report where user_id=?', [req.query.user_id], (err, data) => {
                console.log('删除',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async delbatch(req, resp) {  //批量删除
    let { userName } = req.body;
    console.log(req.query.user_ids);
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('delete from report where user_id in ('+req.query.user_ids+')', [], (err, data) => {
                console.log('批量删除',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },
  //-------------------HXM接口---体检报告信息--------------------------体检报告信息------------------------------

  async getmecindex(req, resp) {  //指标详情pc
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select * from mec_index', [], (err, data) => {
                console.log('指标列表',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getlookindexpcuser(req, resp) {  //指标详情pc段
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('select * from mec_index where index_id=?', [req.query.index_id], (err, data) => {
                console.log('pc端',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async delindex(req, resp) {  //单项删除
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('delete from mec_index where index_id=?', [req.query.index_id], (err, data) => {
                console.log('单相删除',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async addindex(req, resp) {  //体检详情pc段对应user添加
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('INSERT INTO mec_index VALUES(?,?,?,?,?,?,?)', [req.query.index_id,req.query.index_name,req.query.index_unit,req.query.index_up,req.query.index_down,req.query.index_standard,req.query.org_name], (err, data) => {
                console.log('user添加',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async editindex(req, resp) {  //指标修改
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('UPDATE  mec_index set index_name=?,index_unit=?,index_up=?,index_down=?,index_standard=?,org_name=? where index_id=?', [req.query.index_name,req.query.index_unit,req.query.index_up,req.query.index_down,req.query.index_standard,req.query.org_name,req.query.index_id], (err, data) => {
                console.log('指标修改',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async delbatchindex(req, resp) {  //批量删除
    let { userName } = req.body;
    console.log(req.query.user_ids);
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('delete from mec_index where index_id in ('+req.query.index_ids+')', [], (err, data) => {
                console.log('指标批量删除',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async getdimindex(req, resp) {  //查询
    let { userName } = req.body;
    console.log(req);
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('SELECT * FROM mec_index WHERE index_name LIKE "%'+req.query.index_name+'%"', [], (err, data) => {
                console.log('指标查询',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },
//---------------------------------------------HXM指标接口
async cancalorder(req, resp) {  //指标修改
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('UPDATE ordertable set order_status=? where order_orderId=?', [req.query.order_statuse,req.query.order_orderId], (err, data) => {
                console.log('指标修改',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

//--------------------------------------------HXM小程序取消接口
  async addorg(req, resp) {  //添加机构
    console.log(req.body);
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('INSERT INTO organization VALUES(null,?,?,?,?,?,?,0,?,0)', [req.body.org_name,req.body.org_tel,req.body.org_address,req.body.city,req.body.state,req.body.org_image,,req.body.org_attribute,req.body.org_type,req.body.org_people], (err, data) => {
                console.log('添加机构',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },

  async editorg(req, resp) {  //修改机构
    console.log(req.body);
    let { userName } = req.body;
        let userInfo = await new Promise((resolve, reject) => {
            DBConfig.db('UPDATE organization set org_name=?,org_tel=?,org_address=?,city=?,state=?,org_image=?,org_attribute=0,org_type=?,org_people=0 where org_id=?', [req.body.org_name,req.body.org_tel,req.body.org_address,req.body.city,req.body.state,req.body.org_image,req.body.org_type,req.body.org_attribute,req.body.org_id,req.body.org_people], (err, data) => {
                console.log('修改',data);
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        }).catch(()=>{});
    resp.send(userInfo);
  },
};






